package com.third.bank.controller;

import com.third.bank.common.StaffCommon;
import com.third.bank.entity.Staff;
import com.third.bank.service.StaffService;
import com.third.bank.util.BaseResponse;
import com.third.bank.util.JwtUtil;
import com.third.bank.util.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 柜台职员表controller
 */
@RestController
@RequestMapping("/staff")
public class StaffController {

    @Autowired
    private StaffService staffService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 柜台职员登录
     * @return
     */
    @PostMapping("/login")
    public BaseResponse staffLogin(@RequestBody StaffCommon staffCommon){
//        按照柜员账号查询柜员
        Staff byAccount = staffService.findByAccount(staffCommon.getStaffAccount());
        if (byAccount != null){
//            对比密码是否正确
            boolean equals = byAccount.getStaffPassword().equals(staffCommon.getStaffPassword());
            if (equals){
                //                密码输入正确，登陆成功
                Map<String,Object> map = new HashMap<>();
//                生成token
                String token = JwtUtil.createToken(byAccount);
                map.put("loginStaff",byAccount);
                map.put("token",token);
//                将生成的token放入redis
                stringRedisTemplate.opsForValue().set(token, String.valueOf(new Date().getTime()));

                return ResultUtils.success(map);
            }else {
                return ResultUtils.error(404,"密码输入错误");
            }
        }else {
            return ResultUtils.error(404,"账户不存在");
        }

    }
}
